package com.lianniu.mgr.teaAddr.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.teaAddr.dao.TeaUserAddrDao;
import com.lianniu.mgr.teaAddr.model.TeaUserAddr;
import com.lianniu.mgr.teaAddr.service.ITeaUserAddrService;
import com.lianniu.mgr.util.TeaContant;
import com.lianniu.mgr.util.UUIDUtil;



@Service
@Transactional
public class TeaUserAddrServiceImpl implements ITeaUserAddrService {

	private final static Logger logger = Logger.getLogger(TeaUserAddrServiceImpl.class);

	@Autowired
	private TeaUserAddrDao teaUserAddrDao;

	public BaseResult insert(TeaUserAddr record) {
		logger.info("TeaUserAddrServiceImpl insert start");
		if(!"".equals(record.getId())){
			//原先有这个地址
			teaUserAddrDao.deleteByPrimaryKey(record.getId());
		}
		record.setId(UUIDUtil.getUUID());
		if(TeaContant.DEFAULT.equals(record.getIsDefault())){
			teaUserAddrDao.cancleDefault(record.getUserId());
		}
		teaUserAddrDao.insert(record);
		logger.info("TeaUserAddrServiceImpl insert end");
		return BaseResult.success();
	}

	@Override
	public BaseResult getUserAddrList(String userId) {
		logger.info("TeaUserAddrServiceImpl getUserAddrList start");
		List<TeaUserAddr> teaUserList = teaUserAddrDao.getUserAddrList(userId);
		logger.info("TeaUserAddrServiceImpl getUserAddrList end");
		return BaseResult.success(teaUserList);
	}

	@Override
	public BaseResult setDefaultAddr(String userId, String addrId) {
		logger.info("TeaUserAddrServiceImpl insert start");
		teaUserAddrDao.cancleDefault(userId);
		teaUserAddrDao.setDefault(addrId);
		logger.info("TeaUserAddrServiceImpl insert end");
		return BaseResult.success();
	}

	@Override
	public BaseResult deleteAddr(String addrId) {
		logger.info("TeaUserAddrServiceImpl deleteAddr start");
		teaUserAddrDao.deleteByPrimaryKey(addrId);
		logger.info("TeaUserAddrServiceImpl deleteAddr end");
		return BaseResult.success();
	}

	@Override
	public BaseResult getAddrInfo(String addrId) {
		logger.info("TeaUserAddrServiceImpl getAddrInfo start");
		TeaUserAddr teaUserAddr = teaUserAddrDao.selectByPrimaryKey(addrId);
		logger.info("TeaUserAddrServiceImpl getAddrInfo end");
		return BaseResult.success(teaUserAddr);
	}
}
